define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var first = false;
    var Controller = {
        index: function () {
            Table.api.init();
            this.table.first();
        },
        table: {
            first: function () {

                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'category/index' + location.search,
                    extend: {
                        index_url: 'category/index' + location.search,
                        edit_url: 'category/edit',
                        table: 'category',
                    },
                    queryParams: {
                        type: 'recovery'
                    },
                    onLoadSuccess: function (data) {
                        if (!first) {
                            Controller.table.second();
                        }
                        first = true;
                        if (data.total) {
                            $("#myTabContent2 .form-commonsearch input[name='category_id']").val(data.rows[0].id);
                            $("#myTabContent2 .btn-refresh").trigger("click");
                        }
                    },
                    toolbar: '#toolbar1',
                    pk: 'id',
                    sortName: 'id',
                    search: false,
                    commonSearch: false,
                    columns: [
                        [
                            {field: 'id', title: __('Id')},
                            {field: 'name', title: __('Name')},
                            {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, buttons: [
                                {
                                    title: __('Details'),
                                    text: __('Details'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-primary btn-xs btn-click',
                                    click: function (e, data) {
                                        if (!first) {
                                            Controller.table.second();
                                        }
                                        first = true;
                                        $("#myTabContent2 .form-commonsearch input[name='category_id']").val(data.id);
                                        $("#myTabContent2 .btn-refresh").trigger("click");
                                    }
                                }
                                ], formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'recovery/index',
                    extend: {
                        index_url: 'recovery/index' + location.search,
                        add_url: 'recovery/add',
                        del_url: 'recovery/del',
                        edit_url: 'recovery/edit',
                        multi_url: 'recovery/multi',
                        import_url: 'recovery/import'
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: true,
                    pageSize: 100,
                    columns: [
                        [
                            {field: 'id', title: 'ID'},
                            {field: 'category_id', title: __('Category_id'), visible: false},
                            {field: 'category.name', title: __('Category.name'), operate:'LIKE'},
                            {field: 'name', title: __('Name'), operate:'LIKE'},
                            {field: 'unit', title: __('Unit')},
                            {field: 'price', title: __('Price'), operate:'BETWEEN'},
                            {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, events: Table.api.events.operate, formatter: Table.api.formatter.toggle},
                            {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});